Queue Operations using ‘c’
A line is a direct information structure that follows the Earliest in, earliest out (FIFO) guideline. Components are embedded at the back (enqueue) and eliminated from the front (dequeue). Here are the essential tasks performed on a line:
1. Enqueue: Adds a component to the back of the line. The new component turns into the last thing in the line.
2. Dequeue: Eliminates the component from the front of the line. The component that has been in the line for a very long time is taken out, and the front pointer is moved to the following component in the line.
3. Look/Front: Returns the component at the front of the line without eliminating it. It permits you to analyze the following component that will be dequeued.
4. IsEmpty: Checks regardless of whether the line is unfilled. It returns valid assuming the line is unfilled and bogus in any case.
These tasks empower the control and recovery of components in a line. This is an illustration of the way these tasks can be performed on a line:
Beginning line: [ ]
1. Enqueue 1: [1]
2. Enqueue 2: [1, 2]
3. Enqueue 3: [1, 2, 3]
4. Dequeue: [2, 3]
5. Look: 2
6. Dequeue: [3]
7. IsEmpty? Misleading
8. Dequeue: [ ]
9. IsEmpty? Valid
It's vital to take note of that a few varieties of lines might uphold extra tasks like size/count, clear, or iterator usefulness. Also, there can be various executions of lines, like utilizing exhibits, connected records, or roundabout supports, however the fundamental tasks continue as before.
Program:-
#include<stdio.h>
#include<stdlib.h>
struct queue
{
int data;
struct queue *next;
};
struct queue
*front,*rear;
int x;
void
enqueue();
void
dequeue();
int
isempty();
void
display();
void main()
{
int ch,p;
printf("\n
1.display\n2.enqueue\n3.dequeue\n 4.is empty\n 5.exit\n");
while(1)
{
printf("enter your
choise\n");
scanf("%d",&ch);
switch(ch)
{
case 1:display();
break;
case 2:printf("enter
data\n");
scanf("%d",&x);
enqueue();
break;
case 3:dequeue();
break;
case 4:p=isempty();
if(p==1)
{
printf("queue is
empty\n");
}
else
{
printf("queue has some
elements\n");
}
break;
case 5:exit(1);
}
}
}
void
enqueue()
{
struct queue *new;
new=(struct queue *)malloc(sizeof(struct queue
));
new->data=x;
new->next=NULL;
if(rear==NULL)
{
rear=new;
front=new;
}
else
{
rear->next=new;
rear=new;
}
}
void
dequeue()
{
struct queue *t;
if(front==NULL)
printf("queue is empty\n");
else
{
t=front;
printf("deleted element is
%d",front->data);
front=front->next;
free(t);
}
}
int
isempty()
{
if(front==NULL)
return 1;
return 0;
}
void
display()
{
struct queue *t;
t=front;
while(t!=NULL)
{
printf("%d",t->data);
t=t->next;
}
}
Output:
0 Comments